package com.lanou.lenanapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.lanou.lenanapp.R;
import com.lanou.lenanapp.db.DatabaseHelper;
import com.lanou.lenanapp.entity.greendao.CollectEntity;
import com.lanou.lenanapp.entity.greendao.JokeEntity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 收藏段子的适配器
 */
public class CollectAdapter extends RecyclerView.Adapter<CollectAdapter.CollectViewHolder>{

    private List<CollectEntity> collectEntities;
    private List<JokeEntity> jokeEntities;
    private DatabaseHelper helper;
    private Context context;


    public CollectAdapter(Context context) {
        super();
        this.context = context;
    }

    public void addData(List<CollectEntity> datas){
        this.collectEntities = datas;
        notifyDataSetChanged();
    }

    class CollectViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView updata_time_collect, wbody_collect, review_collect, like_collect;
        private ImageView favorited_collect, transmit_joke_collect, like_one_joke, like_image_collect;
        private int position;

        public CollectViewHolder(View itemView) {
            super(itemView);
            helper = DatabaseHelper.getHelper();
            updata_time_collect = (TextView) itemView.findViewById(R.id.updata_time_collect);
            wbody_collect = (TextView) itemView.findViewById(R.id.wbody_collect);
            review_collect = (TextView) itemView.findViewById(R.id.review_collect);
            like_collect = (TextView) itemView.findViewById(R.id.like_collect);
            favorited_collect = (ImageView) itemView.findViewById(R.id.favorited_collect);
            favorited_collect.setOnClickListener(this);
            transmit_joke_collect = (ImageView) itemView.findViewById(R.id.transmit_joke_collect);
            like_one_joke = (ImageView) itemView.findViewById(R.id.like_one_joke);
            like_image_collect = (ImageView) itemView.findViewById(R.id.like_image_collect);
        }
        //点击收藏按钮取消收藏
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.favorited_collect:
                   int position = getLayoutPosition();//获得位置(数组中可以改变的位置)
                   favorited_collect.setImageResource(R.drawable.icon_favorite);
                   CollectEntity collectEntity = collectEntities.get(position);
                   String str = collectEntity.getWid();//获得主键
                   helper.deleteByIdCollect(str);//根据主键删除
                   Toast.makeText(context, "已取消收藏...", Toast.LENGTH_SHORT).show();
                   collectEntities.remove(position);//从集合中移除(适配器中加载显示的是集合中的数据)
                   notifyItemRemoved(position);//刷新
                   if (listener != null){
                       listener.OnRemoveClick(collectEntities);
                   }
                    break;
            }
        }
    }

    @Override
    public CollectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.activity_collect_gut, null);
        return new CollectViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CollectViewHolder holder, int position) {

        if (collectEntities != null && collectEntities.size() > 0) {
            CollectEntity collectEntity = collectEntities.get(position);

            holder.position = position;//获得缓存里的position

            String wo = collectEntity.getWbody();
            holder.wbody_collect.setText(wo);
            String time = getNormalTime(collectEntity.getUpdate_time());
            holder.updata_time_collect.setText(time);//设置时间

            double t = Double.parseDouble(collectEntity.getLikes());
            int m = (int) t;
            holder.like_collect.setText(m + "");
            holder.review_collect.setText(collectEntity.getComments());
        }

    }

    @Override
    public int getItemCount() {
        return collectEntities != null && collectEntities.size() > 0 ? collectEntities.size() : 0;
    }


    public  String  getNormalTime( String value) {
        Long l = Long.valueOf(value);
        l = l * 1000;
        Date date = new Date(l);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        String time = format.format(date) ;
        return time;
    }

    //接口回调 判断收藏里是否有东西
    public interface OnRemoveClickListener{
        void OnRemoveClick(List<CollectEntity> list);
    }
    private OnRemoveClickListener listener;
    public void setOnRemoveClickListener(OnRemoveClickListener listener){
        this.listener = listener;
    }


}
